package com.springmvc.controller;

import com.springmvc.entity.News;
import com.springmvc.service.NewsService;
import com.springmvc.way.Msfile;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;

/**
 * Created by 29047 on 2017/12/22.
 */
@Controller
public class newsController {

    @Autowired
    private NewsService newsService;
    @Autowired
    private HttpServletRequest request;


    //新增新闻接口
    @RequestMapping(value = "/news/add", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject insertNew(@RequestParam("addnews") String addnew, @RequestParam("files") MultipartFile[] files, @RequestParam("attach") MultipartFile[] attach) throws ParseException {
        JSONObject json = new JSONObject();
        JSONObject new_news = JSONObject.fromObject(addnew);
        News news = new News();
        Iterator it = new_news.keys();
        while (it.hasNext()) {
            String key = it.next().toString();
            if (key.equals("news_type")) {
                news.setNewsType(Integer.parseInt(new_news.getString(key)));
            }
            if (key.equals("title")) {
                news.setTitle(new_news.getString(key));
            }
            if (key.equals("content")) {
                news.setContent(new_news.getString(key));
            }
            if (key.equals("author")) {
                news.setAuthor(new_news.getString(key));
            }
            if (key.equals("stick")) {
                news.setStick(Integer.parseInt(new_news.getString(key)));
            }
        }

        Msfile msfile1 = new Msfile(files, request);
        news.setPictureLocation(msfile1.strPic());
        Msfile msfile2 = new Msfile(attach, request);
        news.setAttachmentLocation(msfile2.strPic());
        news.setClick(0);
        Date date = new Date();
        news.setAddDate(date);
        if (newsService.insert(news) == 1) {
            json.put("result", 1);  //新增新闻成功返回1
        } else {
            json.put("result", 0);  //新增新闻失败返回0
        }
        return json;
    }

    //删除新闻接口
    @RequestMapping(value = "/news/delete",method = RequestMethod.POST)
    @ResponseBody
    public JSONObject delNews(@RequestParam("newsId") String newsid){
        JSONObject json = new JSONObject();
        if (newsService.deleteByPrimaryKey(Integer.parseInt(newsid)) == 1){
            json.put("result",1);  //删除新闻成功返回1
        } else {
            json.put("result",0);  //删除新闻失败返回0
        }
        return json;
    }

    //查看新闻详情接口
    @RequestMapping(value = "/news/detail",method = RequestMethod.GET)
    @ResponseBody
    public JSONObject detNews(@RequestParam("newsId") String newsid){
        JSONObject json = new JSONObject();
        News news = newsService.selectByPrimaryKey(Integer.parseInt(newsid));
        news.setClick(news.getClick()+1);
        newsService.updateByPrimaryKey(news);
        json.put("data",news);
        return json;
    }

    //首页查看前五条新闻
    @RequestMapping(value = "/news/firstfive",method = RequestMethod.GET)
    @ResponseBody
    public JSONObject ffnews(){
        JSONObject json = new JSONObject();

        ArrayList<News> ary = newsService.selectFirstNum(5);
        json.put("data",ary);

        return json;
    }

    @RequestMapping(value = "/news/list",method = RequestMethod.GET)
    @ResponseBody
    public JSONObject newList(){
        JSONObject json = new JSONObject();
        for (int i = 1;i <= 6;i++){
            ArrayList<News> list = newsService.selectByType(i);
            json.put("newslist"+i,list);
        }
        return json;
    }



    @RequestMapping(value = "/test",method = RequestMethod.GET)
    public String newtest(){
        return "addTest";
    }
}
